//
//  ZDPhotoBrowserDismissAnimation.swift
//  1026SwiftSinaWeiBo
//
//  Created by Apple on 15/11/10.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class ZDPhotoBrowserDismissAnimation: NSObject, UIViewControllerAnimatedTransitioning {

    
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        
        return 0.5
    }
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        //获取要消失的视图
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        
        //添加到容器视图
        transitionContext.containerView()?.addSubview(fromView)
        
        //获取要dismiss的控制器
        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as! ZDPhotoBrowserViewController
        
        //获取dismiss时的过渡视图
        let tempImageView = fromVC.dismissTempImageView()
        
        //过渡视图添加到容器视图中
        transitionContext.containerView()?.addSubview(tempImageView)
        
        //隐藏collectionView
        fromVC.photoBrowserCollectionView.hidden = true
        
        UIView.animateWithDuration(transitionDuration(nil), animations: { () -> Void in
            
            fromView.alpha = 0
            
            tempImageView.frame = fromVC.dismissTargetFrame()
            
            }) { (_) -> Void in
                
                //结束转场
                transitionContext.completeTransition(true)
        }
    }
    
}
